/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client.view;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.weissenborn.colabtex.client.places.Dashboard;

/**
 * The Class GroupCreationViewImpl.
 */
public class GroupCreationViewImpl extends Composite implements GroupCreationView {

	/**
	 * The Interface Binder.
	 */
	interface Binder extends UiBinder<Widget, GroupCreationViewImpl> {
	}
	
	/** The Constant binder. */
	private static final Binder binder = GWT.create(Binder.class);
	
	/** The member name box. */
	@UiField TextBox memberNameBox;
	
	/** The permission box. */
	@UiField ListBox permissionBox;
	
	/** The member list. */
	@UiField ListBox memberList;
	
	/** The add btn. */
	@UiField Button addBtn;
	
	/** The create btn. */
	@UiField Button createBtn;
	
	/** The cancel btn. */
	@UiField Button cancelBtn;
	
	/** The group nametxtbox. */
	@UiField TextBox groupNametxtbox;

	/** The listener. */
	private Presenter listener;

	/**
	 * Instantiates a new group creation view impl.
	 */
	public GroupCreationViewImpl() {
		initWidget(binder.createAndBindUi(this));
		permissionBox.addItem("Editor");
		permissionBox.addItem("Admin");
		permissionBox.addItem("Read-only");
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupCreationView#addMember(java.lang.String, java.lang.String)
	 */
	public void addMember(String name, String role) {
		memberList.addItem(name+"~"+role);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupCreationView#setPresenter(com.weissenborn.colabtex.client.view.GroupCreationView.Presenter)
	 */
	public void setPresenter(Presenter listener) {
		this.listener = listener;
	}
	
	/**
	 * On add btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("addBtn")
	void onAddBtnClick(ClickEvent event) {
		if(memberNameBox.getText()!=""&&permissionBox.getSelectedIndex()>=0) {
			addMember(memberNameBox.getText(),permissionBox.getItemText(permissionBox.getSelectedIndex()));
		}
			
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupCreationView#getMembers()
	 */
	public Map<String,String> getMembers() {
		Map<String,String> list = new HashMap<String,String>();
		for(int i = 0; i < memberList.getItemCount(); i++) {
			list.put(memberList.getItemText(i).split("~")[0],memberList.getItemText(i).split("~")[1]);
		}
		return list;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupCreationView#getGroupName()
	 */
	public String getGroupName() {
		return groupNametxtbox.getText();
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupCreationView#clearMembers()
	 */
	public void clearMembers() {
		memberList.clear();
	}
	
	/**
	 * On create btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("createBtn")
	void onCreateBtnClick(ClickEvent event) {
		listener.createGroup();
	}
	
	/**
	 * On cancel btn click.
	 *
	 * @param event the event
	 */
	@UiHandler("cancelBtn")
	void onCancelBtnClick(ClickEvent event) {
		listener.goTo(new Dashboard());
	}
}
